﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.ProductInfo;

namespace com.subb.ProductInfo
{
    public class PP_ProductDAL
    {
        public List<PP_Product> GetAllProduct(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_Product.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProductID);
                ttlCount = query.Count();
                List<PP_Product> prodlist = query.Skip(start).Take(limit).ToList();
                return prodlist;
            }
        }

        public List<PP_Product> GetAllProduct(int pcataId, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_Product.Where(c => c.ProdCataID == pcataId).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProductID);
                ttlCount = query.Count();
                List<PP_Product> prodlist = query.Skip(start).Take(limit).ToList();
                return prodlist;
            }
        }
        public List<PP_Product> GetAllUiProduct(int cataid, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            SugardbEntities ctx = new SugardbEntities();
            IQueryable<PP_Product> query = null;
            query = ctx.PP_Product;
            if (cataid != 0)
            {
                query = query.Where(c => c.ProdCataID == cataid);
            }
            query = query.Where(c => c.IsShow == 1).OrderByDescending(c => c.SortNum);
            ttlCount = query.Count();
            List<PP_Product> list = query.Skip(start).Take(limit).ToList();
            return list;
        }

        public PP_Product GetProduct(int prodId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                PP_Product prod = ctx.PP_Product.SingleOrDefault(c => c.ProductID == prodId);
                return prod;
            }
        }

        public PP_Product GetUiProduct(int prodId)
        {
            SugardbEntities ctx = new SugardbEntities();
            PP_Product prod = ctx.PP_Product.SingleOrDefault(c => c.ProductID == prodId && c.IsShow == 1);
            return prod;

        }

        public int CreateProduct(Product v_prod)
        {
            int prodId = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_Product pprod = new PP_Product
                {
                    ProductName = v_prod.ProductName,
                    ProdCataID = v_prod.ProdCataID,
                    ProductType = v_prod.ProductType,
                    UnitPrice = v_prod.UnitPrice,
                    Company = v_prod.Company,
                    Brand = v_prod.Brand,
                    Address = v_prod.Address,
                    AppDomain = v_prod.AppDomain,
                    CaseID = v_prod.CaseID,
                    Keywords = v_prod.Keywords,
                    Description = v_prod.Description,
                    ProductCover = v_prod.ProductCover,
                    IsShow = v_prod.IsShow,
                    SortNum = v_prod.SortNum,
                    CreateTime = DateTime.Now,
                    Creator = v_prod.Creator,
                    ProductLevel = v_prod.ProductLevel,
                    Package = v_prod.Package,
                    ExpDate = v_prod.ExpDate,
                    StoreCondition = v_prod.StoreCondition
                };
                ctx.PP_Product.AddObject(pprod);
                ctx.SaveChanges();
                prodId = pprod.ProductID;
            }
            return prodId;
        }

        public bool UpdateProduct(int prodId, Product v_prod)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_Product pprod = ctx.PP_Product.SingleOrDefault(c => c.ProductID == prodId);
                if (pprod != null)
                {
                    pprod.ProductName = v_prod.ProductName;
                    pprod.ProdCataID = v_prod.ProdCataID;
                    pprod.ProductType = v_prod.ProductType;
                    pprod.UnitPrice = v_prod.UnitPrice;
                    pprod.Company = v_prod.Company;
                    pprod.Brand = v_prod.Brand;
                    pprod.Address = v_prod.Address;
                    pprod.AppDomain = v_prod.AppDomain;
                    pprod.CaseID = v_prod.CaseID;
                    pprod.Keywords = v_prod.Keywords;
                    pprod.Description = v_prod.Description;
                    pprod.ProductCover = v_prod.ProductCover;
                    pprod.IsShow = v_prod.IsShow;
                    pprod.SortNum = v_prod.SortNum;
                    pprod.ProductLevel = v_prod.ProductLevel;
                    pprod.Package = v_prod.Package;
                    pprod.ExpDate = v_prod.ExpDate;
                    pprod.StoreCondition = v_prod.StoreCondition;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveProduct(int prodId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_Product pprod = ctx.PP_Product.SingleOrDefault(c => c.ProductID == prodId);
                if (pprod != null)
                {
                    if (pprod.PP_ProductImg != null)
                    {
                        List<PP_ProductImg> pimgList = pprod.PP_ProductImg.ToList();
                        int count = pimgList.Count;
                        for (int i = count - 1; i >= 0; i--)
                        {
                            ctx.PP_ProductImg.DeleteObject(pimgList[i]);
                        }
                    }
                    ctx.PP_Product.DeleteObject(pprod);
                    ctx.SaveChanges();
                }
            }
            return result;
        }


        /// <summary>
        /// 获取最新的产品列表
        /// </summary>
        /// <param name="limit"></param>
        /// <returns></returns>
        public List<PP_Product> GetUiLastestProduct(int limit)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_Product.OrderByDescending(c => c.ProductID);
                List<PP_Product> prodlist = query.Take(limit).ToList();
                return prodlist;
            }
        }

        public List<PP_Product> SearchByName(string key, int start,int limit,out int ttlCount, string lang)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.PP_Product.Where(c => c.ProductName.Contains(key) && c.PP_ProductCata!=null && c.PP_ProductCata.Lang==lang).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProductID);
            ttlCount = query.Count();
            List<PP_Product> prodList = query.Skip(start).Take(limit).ToList();
            return prodList;
        }

        public List<PP_Product> SearchByDescr(string key, int start, int limit, out int ttlCount)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.PP_Product.Where(c => c.Description.Contains(key)).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProductID);
            ttlCount = query.Count();
            List<PP_Product> prodList = query.Skip(start).Take(limit).ToList();
            return prodList;
        }
    }
}
